{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import platform\n",
    "print(\"PyTorch version:{}\".format(torch.__version__))\n",
    "print(\"Python version:{}\".format(platform.python_version()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第5章 Python基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.2.5 Jupyter Notebook 的使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Code模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Markdown模式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hello World!\n",
    "## Hello World!\n",
    "### Hello World!\n",
    "Hello World!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### LaTex语法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$f(x) = 3x+7$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.1 Python 的基本语法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 打印输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello,World\")\n",
    "print(\"-\"*10)\n",
    "print(\"Hello\")\n",
    "print(\"World\")\n",
    "print(\"-\"*10)\n",
    "print(\"Hello\",\"World\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 代码中的注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#一行注释\n",
    "print(\"Hello,World\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "多行注释\n",
    "多行注释\n",
    "多行注释\n",
    "\"\"\"\n",
    "'''\n",
    "多行注释\n",
    "多行注释\n",
    "多行注释\n",
    "'''\n",
    "print(\"Hello,World\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 代码的缩进"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello,World\")\n",
    " print(\"Hello,World\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    print(i)\n",
    "    print(\"-\"*10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多行语句的分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello,World.Hello,World.\\\n",
    "Hello,World.Hello,World.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.2 Python变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int_num = 10\n",
    "float_num = 10.00\n",
    "string = \"Hello,World\"\n",
    "\n",
    "print(int_num)\n",
    "print(float_num)\n",
    "print(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string1 = string2 = string3 = \"Hello,World\"\n",
    "\n",
    "print(string1)\n",
    "print(string2)\n",
    "print(string3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string1, string2, string3 = \"Hello\", \"World\",\"Hello,World\"\n",
    "\n",
    "print(string1)\n",
    "print(string2)\n",
    "print(string3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string1, string2, string3 = \"Hello\",\"Hello,World\",\"World\"\n",
    "\n",
    "print(string1)\n",
    "print(string2)\n",
    "print(string3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.3 常用的数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int_num = 10\n",
    "float_num = 10.00\n",
    "\n",
    "print(int_num)\n",
    "print(float_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "float_num = 10.000\n",
    "\n",
    "print(float_num)\n",
    "print(\"%f\" % float_num)\n",
    "print(\"%.2f\" % float_num)\n",
    "print(\"%.4f\" % float_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string1 = 'Hello,World_@1'\n",
    "string2 = \"Hello,World_@2\"\n",
    "\n",
    "print(string1)\n",
    "print(string2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = 'Hello,World'\n",
    "string0 = string[0]\n",
    "string1 = string[0:13]\n",
    "string2 = string[0:5]\n",
    "string3 = string[-1]\n",
    "string4 = string[-5:]\n",
    "string5 = string[6:]\n",
    "string6 = string[:5]\n",
    "\n",
    "print(string0)\n",
    "print(string1)\n",
    "print(string2)\n",
    "print(string3)\n",
    "print(string4)\n",
    "print(string5)\n",
    "print(string6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list1 = [ \"Hello,World\", 100 , 10.00 ]\n",
    "list2 = [123, 'Hi']\n",
    "\n",
    "print(list1) # 输出整个list1 列表元素\n",
    "print(list1[0]) # 输出列表的第1 个元素\n",
    "print(list1[1:]) # 输出从第1 个索引开始至列表末尾的所有元素\n",
    "print(list1[-1]) # 输出列表的最后一个元素\n",
    "print(list1 + list2) # 输出列表的组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_old = [ \"Hello,World\", 100 , 10.00 ]\n",
    "print(list_old) # 输出整个list_old 列表元素\n",
    "\n",
    "list_old[0] = \"Hello,World,Hi\" # 对list_old 的第1 个元素重新赋值\n",
    "list_new = list_old\n",
    "print(list_new) # 输出整个list_old 列表元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple1 = (\"Hello,World\", 100 , 10.00 )\n",
    "tuple2 = (123, 'Hi')\n",
    "\n",
    "print(tuple1) # 输出整个tuple1 元组元素\n",
    "print(tuple1[0]) # 输出元组的第1 个元素\n",
    "print(tuple1[1:]) # 输出从第1 个索引开始至列表末尾的所有元素\n",
    "print(tuple1[-1]) # 输出元组的最后一个元素\n",
    "print(tuple1 + tuple2) # 输出元组的组合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_1 = {}\n",
    "dict_1[\"one\"] = \"This is one\"\n",
    "dict_1[2] = \"This is two\"\n",
    "dict_info = {\"name\": \"Tang\", \"num\":7272, \"city\": \"GL\"}\n",
    "\n",
    "print (dict_1[\"one\"]) # 输出键值为one 的值\n",
    "print (dict_1[2]) # 输出键值为 2 的值\n",
    "print (dict_info) # 输出整个dict_info 字典\n",
    "print (dict_info.keys()) # 输出dict_info 的所有键值\n",
    "print (dict_info.values()) # 输出dict_info 的所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.4 Python运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 算术运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5 #定义变量a\n",
    "b = 2 #定义变量b\n",
    "\n",
    "c = a + b #进行加法运算\n",
    "print(\"a + b =\", c)\n",
    "\n",
    "c = a - b #进行减法运算\n",
    "print(\"a - b =\", c)\n",
    "\n",
    "c = a / b #进行除法运算\n",
    "print(\"a / b =\", c)\n",
    "\n",
    "c = a * b #进行乘法运算\n",
    "print(\"a * b =\", c)\n",
    "\n",
    "c = a // b #进行取整运算\n",
    "print(\"a // b =\", c)\n",
    "\n",
    "c = a % b #进行取模运算\n",
    "print(\"a % b =\", c)\n",
    "\n",
    "c = a ** b #进行幂运算\n",
    "print(\"a ** b =\", c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5 #定义变量a\n",
    "b = 2 #定义变量b\n",
    "\n",
    "a += b #进行加法运算\n",
    "print(\"a + b =\", a)\n",
    "\n",
    "a = 5 #对变量进行重置\n",
    "a -= b #进行减法运算\n",
    "print(\"a - b =\", a)\n",
    "\n",
    "a = 5 #对变量进行重置\n",
    "a /= b #进行除法运算\n",
    "print(\"a / b =\", a)\n",
    "\n",
    "a = 5 #对变量进行重置\n",
    "a *= b #进行乘法运算\n",
    "print(\"a * b =\", a)\n",
    "\n",
    "a = 5 #对变量进行重置\n",
    "a //= b #进行取整运算\n",
    "print(\"a // b =\", a)\n",
    "\n",
    "a = 5 #对变量进行重置\n",
    "a %= b #进行取模运算\n",
    "print(\"a % b =\", a)\n",
    "\n",
    "a = 5 #对变量进行重置\n",
    "a **= b #进行幂运算\n",
    "print(\"a ** b =\", a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 比较运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5 #定义变量a\n",
    "b = 2 #定义变量b\n",
    "\n",
    "print(a == b) #判断a 是否等于b\n",
    "print(a != b) #判断a 是否不等于b\n",
    "print(a > b) #判断a 是否大于b\n",
    "print(a >= b) #判断a 是否大于等于b\n",
    "print(a < b) #判断a 是否小于b\n",
    "print(a <= b) #判断a 是否小于等于b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5 #定义变量a\n",
    "b = 2 #定义变量b\n",
    "c = 4 #定义变量c\n",
    "\n",
    "print(a == b == c) #判断a 是否等于b 且b 等于c\n",
    "print(a > b > c) #判断a 是否大于b 且b 大于c\n",
    "print(a < b <c) #判断a 是否小于b 且b 小于c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 布尔运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True #定义变量a\n",
    "b = False #定义变量b\n",
    "\n",
    "print(a and b) #与预算\n",
    "print(a or b) #或运算\n",
    "print(not a) #非运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2 #定义变量a\n",
    "b = 1 #定义变量b\n",
    "c = 3 #定义变量c\n",
    "\n",
    "print(a>b and a<b) #与预算\n",
    "print(a>b or a<b) #或运算\n",
    "print(not a<b) #非运算\n",
    "print(not a>b) #非运算\n",
    "print(a>b and c<b and c>a)\n",
    "print(a>b or a<b and c>a)\n",
    "print(a>b or a<b or c>a)\n",
    "print(a>b and a<b or c>a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 成员运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_1 = [\"I\",\"am\",\"super\",\"man\"]\n",
    "a = \"super\"\n",
    "b = 1\n",
    "\n",
    "print(a in list_1)\n",
    "print(b in list_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5. 身份运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 500 #定义变量a\n",
    "b = 500 #定义变量b\n",
    "print(\"a 的内存地址：\",id(a))\n",
    "print(\"b 的内存地址：\",id(b))\n",
    "print(\"a is b\",a is b)\n",
    "print(\"a is not b\",a is not b)\n",
    "print(\"a == b\",a == b)\n",
    "\n",
    "a = 10 #定义变量a\n",
    "b = 10 #定义变量b\n",
    "print(\"a 的内存地址：\",id(a))\n",
    "print(\"b 的内存地址：\",id(b))\n",
    "print(\"a is b\",a is b)\n",
    "print(\"a is not b\",a is not b)\n",
    "print(\"a == b\",a == b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.5 Python条件判断语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 10\n",
    "if number == 10: # 条件判断语句\n",
    "    print(\"The number is equal\", number) # 输出符合条件判断语句的代码块\n",
    "else: # 条件判断语句\n",
    "    print(\"The number is not equal 10.\") # 输出符合条件判断语句的代码块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 15\n",
    "if number == 10: # 条件判断语句\n",
    "    print(\"The number is equal\", number) # 输出符合条件判断语句的代码块\n",
    "elif number > 10: # 条件判断语句\n",
    "    print(\"The number is greater than 10.\") # 输出符合条件判断语句的代码块\n",
    "elif number < 10: # 条件判断语句\n",
    "    print(\"The number is less than 10.\") # 输出符合条件判断语句的代码块\n",
    "else: # 条件判断语句\n",
    "    print(\"Error.\") # 输出符合条件判断的代码块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.6 Python循环语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 0\n",
    "while (number < 10):\n",
    "    print( \"The number is\", number)\n",
    "    number += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 10\n",
    "for i in range(10):\n",
    "    if i<number:\n",
    "        print( \"The number is\", i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 10\n",
    "for i in range(10):\n",
    "    if i == 5:\n",
    "        break\n",
    "    if i<number:\n",
    "        print( \"The number is\", i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 10\n",
    "for i in range(10):\n",
    "    if i == 5:\n",
    "        continue\n",
    "    if i<number:\n",
    "        print( \"The number is\", i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 10\n",
    "for i in range(10):\n",
    "    if i == 5:\n",
    "        pass\n",
    "    if i<number:\n",
    "        print( \"The number is\", i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_list = [1,2,3,4,5,6]\n",
    "for i in a_list:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.7 Python中的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def function():\n",
    "    print(\"Hello,World.\")\n",
    "    return\n",
    "\n",
    "a = function()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def function(string = \"Hi\"):\n",
    "    print(\"What you say is:\", string)\n",
    "    return\n",
    "\n",
    "function()\n",
    "function(string = \"Hello,World.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 函数的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def function1(string): #定义必备参数\n",
    "    print(\"What you say is:\", string)\n",
    "    return\n",
    "\n",
    "def function2(string = \"Hi\"): #定义默认参数\n",
    "    print(\"What you say is:\", string)\n",
    "    return\n",
    "\n",
    "def function3(string2 = \"World\",string1 = \"Hello\"): #定义关键字参数\n",
    "    print(\"What you say is:\", string1, string2)\n",
    "    return\n",
    "\n",
    "def function4(arg1, *arg2): #定义不定长参数\n",
    "    print(arg1)\n",
    "    for i in arg2:\n",
    "        print(i)\n",
    "    return\n",
    "\n",
    "function1(\"Hello,World.\")\n",
    "function2()\n",
    "function3()\n",
    "function4(10, 1, 2, 3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.8 Python中的类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 类的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student:\n",
    "    \n",
    "    student_Count = 0\n",
    "    \n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        Student.student_Count += 1\n",
    "    \n",
    "    def dis_student(self):\n",
    "        print(\"Student name:\",self.name,\"Student age:\",self.age)\n",
    "        \n",
    "student1 = Student(\"Tang\", \"20\") #创建第1 个Student 对象\n",
    "student2 = Student(\"Wu\", \"22\") #创建第2 个Student 对象\n",
    "\n",
    "student1.dis_student()\n",
    "student2.dis_student()\n",
    "print(\"Total Student:\", Student.student_Count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 类的继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class People:\n",
    "    \n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        \n",
    "    def dis_name(self):\n",
    "        print(\"name is:\",self.name)\n",
    "\n",
    "    def set_age(self, age):\n",
    "        self.age = age\n",
    "        \n",
    "    def dis_age(self):\n",
    "        print(\"age is:\",self.age)\n",
    "        \n",
    "class Student(People):\n",
    "    def __init__(self, name, age, school_name):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.school_name = school_name\n",
    "\n",
    "    def dis_student(self):\n",
    "        print(\"school name is:\",self.school_name)\n",
    "        \n",
    "student = Student(\"Wu\", \"20\", \"GLDZ\") #创建一个Student 对象\n",
    "student.dis_student() #调用子类的方法\n",
    "student.dis_name() #调用父类的方法\n",
    "student.dis_age() #调用父类的方法\n",
    "student.set_age(22) #调用父类的方法\n",
    "student.dis_age() #调用父类的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 类的重写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Parent: #定义父类\n",
    "    \n",
    "    def __init__(self):\n",
    "        pass\n",
    "    \n",
    "    def print_info(self):\n",
    "        print(\"This is Parent.\")\n",
    "        \n",
    "class Child(Parent): #定义子类\n",
    "    \n",
    "    def __init__(self):\n",
    "        pass\n",
    "\n",
    "    def print_info(self): #对父类的方法进行重写\n",
    "        print(\"This is Child.\")\n",
    "        \n",
    "child = Child()\n",
    "child.print_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.4.2 多维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 创建多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.array([[1,2,3],[4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.array(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.ones([2,3]) #创建全1 的数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,3]) #创建全1 的数组：\n",
    "a[1,2] = 2 #对数组中的元素进行覆盖\n",
    "\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.zeros([2,3]) #创建全0 的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.empty([2, 3]) #创建随机初始化的数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 多维数组的常用属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,3]) #创建全1 的数组\n",
    "a.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,3]) #创建全1 的数组\n",
    "print(a)\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.matrix([[2,3],[3,4]]) #创建矩阵\n",
    "print(a)\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,3]) #创建全1 的数组\n",
    "print(a.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,3]) #创建全1 的数组\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,3], dtype= np.int32) #创建全1 的数组\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,3], dtype= np.int32) #创建全1 的数组\n",
    "a.itemsize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 数组的打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.ones([2,5,3]) #创建全1 的数组\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.arange(2000) #创建有2000 个元素的数组\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.4.3 多维数组的基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 数组的算术运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "\n",
    "print(\"a - b =\",a-b) #打印a-b 的结果\n",
    "print(\"a + b =\",a+b) #打印a+b 的结果\n",
    "print(\"a / b =\",a/b) #打印a/b 的结果\n",
    "print(\"a * b =\",a*b) #打印a*b 的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "\n",
    "print(\"a * b =\",a*b) #打印a*b 的结果\n",
    "\n",
    "c = a.dot(b)\n",
    "print(\"Matrix1: a * b =\",c) #打印a*b 的结果\n",
    "\n",
    "d = np.dot(a,b)\n",
    "print(\"Matrix2: a * b =\",c) #打印a*b 的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3])\n",
    "\n",
    "print(\"a * 2 =\",a*2)\n",
    "print(\"a / 2 =\",a/2)\n",
    "print(\"a - 2 =\",a-2)\n",
    "print(\"a + 2 =\",a+2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数组的自身运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3])\n",
    "\n",
    "print(\"min of array:\",a.min())\n",
    "print(\"max of array:\",a.max())\n",
    "print(\"sum of array:\",a.sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3],\n",
    "[3,2,1]])\n",
    "\n",
    "print(\"min of array:\",a.min())\n",
    "print(\"min of array:\",a.min(axis=0))\n",
    "print(\"min of array:\",a.min(axis=1))\n",
    "print(\"max of array:\",a.max())\n",
    "print(\"max of array:\",a.max(axis=0))\n",
    "print(\"max of array:\",a.max(axis=1))\n",
    "print(\"sum of array:\",a.sum())\n",
    "print(\"sum of array:\",a.sum(axis=0))\n",
    "print(\"sum of array:\",a.sum(axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3]])\n",
    "\n",
    "print(\"Exp of array:\",np.exp(a))\n",
    "print(\"Sqrit of array:\",np.sqrt(a))\n",
    "print(\"Square of array:\",np.square(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.random.seed(42)\n",
    "\n",
    "print(np.random.rand(2,3))\n",
    "print(np.random.randn(2,3))\n",
    "print(np.random.randint(1,10))\n",
    "print(np.random.binomial(6,1))\n",
    "print(np.random.beta(2,3))\n",
    "print(np.random.normal(2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 索引、切片和迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(10)\n",
    "\n",
    "print(a) #输出整个数组\n",
    "print(a[:5]) #输出数组的前五个元素\n",
    "\n",
    "for i in a: #迭代输出数组的全部元素\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3],\n",
    "        [4,5,6],\n",
    "        [7,8,9]])\n",
    "\n",
    "print(a) #输出整个数组\n",
    "print(\"-\"*10)\n",
    "print(a[1]) #输出指定维度的数据\n",
    "print(\"-\"*10)\n",
    "print(a[0:2, 1:3]) #输出指定维度的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3],\n",
    "        [4,5,6],\n",
    "        [7,8,9]])\n",
    "\n",
    "for i in a:\n",
    "    print(i)\n",
    "    \n",
    "print(\"-\"*10)    \n",
    "for i in a:\n",
    "    for j in i:\n",
    "        print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in a.flat:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.5.2 创建图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 线型图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline\n",
    "\n",
    "np.random.seed(42)\n",
    "x = np.random.randn(30)\n",
    "plt.plot(x, \"r--o\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 线条颜色、标记形状和线型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "np.random.seed(42)\n",
    "%matplotlib inline\n",
    "\n",
    "a = np.random.randn(30)\n",
    "b = np.random.randn(30)\n",
    "c = np.random.randn(30)\n",
    "d = np.random.randn(30)\n",
    "plt.plot(a, \"r--o\", b, \"b-*\", c, \"g-.+\", d, \"m:x\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 标签和图例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline\n",
    "\n",
    "np.random.seed(42)\n",
    "x = np.random.randn(30)\n",
    "y = np.random.randn(30)\n",
    "plt.title(\"Example\")\n",
    "plt.xlabel(\"X\")\n",
    "plt.ylabel(\"Y\")\n",
    "\n",
    "X, = plt.plot(x, \"r--o\")\n",
    "Y, = plt.plot(y, \"b-*\")\n",
    "plt.legend([X, Y], [\"X\", \"Y\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 子图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "np.random.seed(42)\n",
    "%matplotlib inline\n",
    "\n",
    "a = np.random.randn(30)\n",
    "b = np.random.randn(30)\n",
    "c = np.random.randn(30)\n",
    "d = np.random.randn(30)\n",
    "\n",
    "fig = plt.figure()\n",
    "ax1 = fig.add_subplot(2,2,1)\n",
    "ax2 = fig.add_subplot(2,2,2)\n",
    "ax3 = fig.add_subplot(2,2,3)\n",
    "ax4 = fig.add_subplot(2,2,4)\n",
    "\n",
    "A, = ax1.plot(a, \"r--o\")\n",
    "ax1.legend([A], [\"A\"])\n",
    "B, = ax2.plot(b, \"b-*\")\n",
    "ax2.legend([B], [\"B\"])\n",
    "C, = ax3.plot(c, \"g-.+\")\n",
    "ax3.legend([C], [\"C\"])\n",
    "D, = ax4.plot(d, \"m:x\")\n",
    "ax4.legend([D], [\"D\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5. 散点图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline\n",
    "\n",
    "np.random.seed(42)\n",
    "x = np.random.randn(30)\n",
    "y = np.random.randn(30)\n",
    "\n",
    "plt.scatter(x,y, c=\"g\", marker=\"o\", label=\"(X,Y)\")\n",
    "plt.title(\"Example\")\n",
    "plt.xlabel(\"X\")\n",
    "plt.ylabel(\"Y\")\n",
    "plt.legend(loc=1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6. 直方图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline\n",
    "\n",
    "np.random.seed(42)\n",
    "x = np.random.randn(1000)\n",
    "\n",
    "plt.hist(x,bins=20,color=\"g\")\n",
    "plt.title(\"Example\")\n",
    "plt.xlabel(\"X\")\n",
    "plt.ylabel(\"Y\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7. 饼图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "labels = ['Dos', 'Cats', 'Birds']\n",
    "sizes = [15, 50, 35]\n",
    "\n",
    "plt.pie(sizes, explode=(0, 0, 0.1), labels=labels, autopct='%1.1f%%',startangle=90)\n",
    "plt.axis('equal')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
